<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" class="w-72">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/手机号/卡号/身份证号" />
        </a-form-item>
        <a-form-item name="dateRange" label="创建日期" class="!w-[300px] ml-3">
          <a-range-picker v-model:value="formState.dateRange" arrow-point-at-center allow-clear format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-button type="primary" class="mb-2" @click="onClickAdd()">建档</a-button>
      <!-- 收费标准 -->
      <BaseTable
        :loading="loading"
        column-code="00001135"
        row-key="brId"
        fill
        :scroll="{ x: 2650 }"
        :data-source="tableData"
        bordered
        :pagination="pagination"
        :exclude-columns="getSystemConfig('ENABLE_CHARGE_STANDARD_MODE') === '1' ? [] : ['chargeStdName']"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'brAge'">
            <span v-if="record.brAge === '0' || record.brAge === 0 || record.brAge === '0岁'">-</span>
            <span v-else>{{ record.brAge }}</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickAdd(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onPrint(record)"> 客户标签 </a-button>
            <a-divider type="vertical" />
            <BaseButton confirm-text="是否确认登记?" type="link" size="small" @click="onClickRegister(record)"> 登记 </BaseButton>
            <!-- <a-button type="link" size="small" @click="onClickRegister(record)"> 登记 </a-button> -->
            <!-- <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onClickDeposit(record)"> 收取押金 </a-button> -->
          </template>
        </template>
      </BaseTable>
    </a-card>
    <PatientFiles v-model:open="editOpen" :row-data="rowData" is-save @success="onSuccess" />
    <RegisterModal v-model:open="registerOpen" :row-data="rowData" title="挂号" @success="onSuccess" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { get, post } from '@/utils/request'
import { getAgeUnit } from '@/utils/format'
import SearchContainer from '@/components/SearchContainer'
import { getSystemConfig } from '@/utils/systemConfig'
import RegisterModal from '@/components/RegisterModal'
import PatientFiles from '../components/PatientFiles'
import useStore from '../store'

const store = useStore()

const route = useRoute()
// const router = useRouter()
const rowData = ref({})
const editOpen = ref(false)
const registerOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [],
  brId: '',
  brMob: '',
  brName: '',
  brSfz: '',
  pyjm: '',
  ofAll: 1
})
const onClickAdd = (row = {}) => {
  rowData.value = { ...row }
  editOpen.value = true
}
const onClickRegister = async (row) => {
  const { code, msg } = await post('/outp/mzdj/doCheckIn', {
    ...row,
    brAge: getAgeUnit(row.brAge).brAge,
    ageUnit: getAgeUnit(row.brAge).ageUnit,
    sfdjjr: '1'
  })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  getData()
  store.patientInfo = { ...row }
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzdj/getPatientInfoList',
  params: formState.value,
  immediate: true,
  formatParams: (params) => {
    return {
      ...params,
      createTimeStdate: params.dateRange?.[0],
      createTimeEddate: params.dateRange?.[1]
    }
  }
})

// const onClickDeposit = (row) => {
//   store.patientInfo = { ...row }
//   router.push({
//     path: '/out-registration/deposit',
//     query: {
//       status: 1
//     }
//   })
// }
// 打印
const onPrint = async (row) => {
  const { code, msg, data } = await post('/outp/outPrint/getMedicalRecordLabelPrint', { brId: row.brId })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
//获取患者信息
const getPatientInfo = async () => {
  if (route.query?.id) {
    const { code, data } = await get('/common/patient/getPatientInfo?br_id=' + route.query.id)
    if (code !== 0) return
    rowData.value = { ...data }
    formState.value.jksy = 2
    editOpen.value = true
  }
}
const onSuccess = () => {
  rowData.value = {}
  getData()
}
onMounted(async () => {
  if (route.query?.id) {
    formState.value.pyjm = route.query.id
    getPatientInfo()
  }
})
</script>
